package com.xomily.devops.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xomily.common.utils.DateUtils;
import com.xomily.common.utils.sign.Md5Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xomily.devops.mapper.DevHostMapper;
import com.xomily.devops.mapper.DevMemLogMapper;
import com.xomily.devops.domain.DevAlarmLog;
import com.xomily.devops.domain.DevHost;
import com.xomily.devops.domain.DevMemLog;
import com.xomily.devops.service.IDevAlarmLogService;
import com.xomily.devops.service.IDevMemLogService;
import com.xomily.system.service.ISysConfigService;

/**
 * 内存日志Service业务层处理
 * 
 * @author xomily
 * @date 2021-11-29
 */
@Service
public class DevMemLogServiceImpl implements IDevMemLogService 
{
    @Autowired
    private DevMemLogMapper devMemLogMapper;
    @Autowired
    private DevHostMapper devHostMapper;
    @Autowired
    private IDevAlarmLogService alarmLogService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询内存日志
     * 
     * @param id 内存日志主键
     * @return 内存日志
     */
    @Override
    public DevMemLog selectDevMemLogById(Long id)
    {
        return devMemLogMapper.selectDevMemLogById(id);
    }

    /**
     * 查询内存日志列表
     * 
     * @param devMemLog 内存日志
     * @return 内存日志
     */
    @Override
    public List<DevMemLog> selectDevMemLogList(DevMemLog devMemLog)
    {
        return devMemLogMapper.selectDevMemLogList(devMemLog);
    }
    
    /**
     * 查询内存日志列表
     * 
     * @param Map 查询条件
     * @return 内存日志集合
     */

    public Map<String, Object> selectDevMemLogByGraph(Map<String, Object> params) {
		double[] data = new double[240];
		String[] xAxis = new String[240];
		List<DevMemLog> logList = devMemLogMapper.selectDevMemLogByGraph(params);
		int h,m,n;
		
		for(int i=0; i<240; i++){
			h = (i/10);
			m = (i%10*6);
			xAxis[i]= String.format("%02d",h)+":"+String.format("%02d",m); 
		} 
		
		for (DevMemLog log : logList) {
            Date createTime = log.getCreateTime();
            h = DateUtils.getHour(createTime);
            m = DateUtils.getMinute(createTime); 
            n = (h*60 + m)/6;			
			if (log.getUseRate()>data[n]) {
				data[n]= log.getUseRate();
			}
		}		
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("memGraph",data);
        ret.put("xAxis",xAxis);
		return ret;
	}

    /**
     * 新增内存日志
     * 
     * @param devMemLog 内存日志
     * @return 结果
     */
    @Override
    public int insertDevMemLog(DevMemLog devMemLog)
    {
        DevHost hostQuery = devHostMapper.selectDevHostByIp(devMemLog.getIp());
        if (hostQuery != null) {
         	DevHost host = new DevHost();
        	host.setId(hostQuery.getId());
           	host.setMem(devMemLog.getTotal());
            host.setMemRate(devMemLog.getUseRate());
            host.setUpdateTime(DateUtils.getNowDate());
        	devHostMapper.updateDevHostMem(host);   
        }
        
	    double memUseRate = Double.parseDouble(configService.selectConfigByKey("dev.host.memUseRate"));
        if ((memUseRate>0) && (devMemLog.getUseRate()>memUseRate)) {
        	DevAlarmLog alarmLog = new DevAlarmLog();
            String content = "内存使用率大于告警值"+ configService.selectConfigByKey("dev.host.memUseRate")+"%";
        	alarmLog.setType("MemWarn");
            alarmLog.setIp(devMemLog.getIp());
            alarmLog.setCreateTime(DateUtils.getNowDate());
            alarmLog.setGenerateTime(devMemLog.getGenerateTime());
            alarmLog.setContent(content);
            alarmLog.setCode( Md5Utils.hash(alarmLog.getType()+alarmLog.getContent()));
            alarmLogService.insertDevAlarmLog(alarmLog);
        }
        devMemLog.setHostId(hostQuery.getId());
        devMemLog.setCreateTime(DateUtils.getNowDate());
        return devMemLogMapper.insertDevMemLog(devMemLog);
    }

    /**
     * 修改内存日志
     * 
     * @param devMemLog 内存日志
     * @return 结果
     */
    @Override
    public int updateDevMemLog(DevMemLog devMemLog)
    {
        return devMemLogMapper.updateDevMemLog(devMemLog);
    }

    /**
     * 批量删除内存日志
     * 
     * @param ids 需要删除的内存日志主键
     * @return 结果
     */
    @Override
    public int deleteDevMemLogByIds(Long[] ids)
    {
        return devMemLogMapper.deleteDevMemLogByIds(ids);
    }

    /**
     * 删除内存日志信息
     * 
     * @param id 内存日志主键
     * @return 结果
     */
    @Override
    public int deleteDevMemLogById(Long id)
    {
        return devMemLogMapper.deleteDevMemLogById(id);
    }
}
